/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 *
 * @author Administrator
 */
public class RealDeviceInfoDao {

    private Connection conn = null;
    public int size;

    public RealDeviceInfoDao(Connection conn) {
        this.conn = conn;
    }

    public boolean doCreate(RealDeviceInfoBean realDeviceInfo) throws Exception{
        boolean flag = false;
        PreparedStatement pstmt = null;
        
        String sql = "INSERT INTO [realdevicedata]([orgcode],[beltspeed] ,[flowspeed] ,[lasttotal],[currenttotal] ,[initialvalueofhour],[currenthourout],[time])"
                + " VALUES (?,?,?,?,?,?,?,?)";
        try {
            pstmt = this.conn.prepareStatement(sql);
            pstmt.setString(1, realDeviceInfo.getOrgcode());
            pstmt.setDouble(2, realDeviceInfo.getBeltspeed());
            pstmt.setDouble(3, realDeviceInfo.getFlowspeed());
            pstmt.setDouble(4, realDeviceInfo.getLasttotal());
            pstmt.setDouble(5, realDeviceInfo.getCurrenttotal());
            pstmt.setDouble(6, realDeviceInfo.getInitialvalueofhour());
            pstmt.setDouble(7, realDeviceInfo.getCurrenthourout());
            pstmt.setString(8, realDeviceInfo.getTime());
            int len = pstmt.executeUpdate();
            if (len > 0) {
                flag = true;
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return flag;
    }
    
    public boolean doDeleteByOrgCode(String orgCode) throws Exception {
        boolean flag = false;
        PreparedStatement pstmt = null;
        String sql = "DELETE FROM realdevicedata WHERE orgcode like '" + orgCode + "%' ";
        try {
            pstmt = this.conn.prepareStatement(sql);
            //pstmt.setString(1,orgCode);
            int len = pstmt.executeUpdate();
            if (len > 0) {
                flag = true;
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return flag;
    }

    public boolean doDeleteByOrgCode(String[] orgCodes) throws Exception {
        for (String oid : orgCodes) {
            doDeleteByOrgCode(oid);
        }
        return true;
    }

    public boolean doUpdate(RealDeviceInfoBean realDeviceInfo) throws Exception {
        boolean flag = false;
        PreparedStatement pstmt = null;
        String sql = null;
        sql = "UPDATE [realdevicedata] SET [orgcode]=?,[beltspeed] =?,[flowspeed]=? ,[lasttotal]=?,[currenttotal]=? ,[initialvalueofhour]=?,[currenthourout]=?,[time]=? WHERE [id]=?";

        try {

            pstmt = this.conn.prepareStatement(sql);
            
            pstmt.setString(1, realDeviceInfo.getOrgcode());
            pstmt.setDouble(2, realDeviceInfo.getBeltspeed());
            pstmt.setDouble(3, realDeviceInfo.getFlowspeed());
            pstmt.setDouble(4, realDeviceInfo.getLasttotal());
            pstmt.setDouble(5, realDeviceInfo.getCurrenttotal());
            pstmt.setDouble(6, realDeviceInfo.getInitialvalueofhour());
            pstmt.setDouble(7, realDeviceInfo.getCurrenthourout());
            pstmt.setString(8, realDeviceInfo.getTime()); 
            pstmt.setLong(9, realDeviceInfo.getId());
            int len = pstmt.executeUpdate();
            if (len > 0) {
                flag = true;
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return flag;
    }

    public boolean doDelete(long pid) throws Exception {
        boolean flag = false;
        PreparedStatement pstmt = null;
        String sql = "DELETE FROM realdevicedata WHERE id=?";
        try {
            pstmt = this.conn.prepareStatement(sql);
            pstmt.setLong(1, pid);
            int len = pstmt.executeUpdate();
            if (len > 0) {
                flag = true;
            }
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return flag;
    }

    public boolean doDelete(String[] pids) throws Exception {
        for (String pid : pids) {
            long id = Long.parseLong(pid);
            doDelete(id);
        }
        return true;
    }

    public RealDeviceInfoBean findByOrgCode(String orgCode) throws Exception{
        RealDeviceInfoBean realDeviceData = null;
        List<RealDeviceInfoBean> all = findAllByOrgCode(orgCode);
        if(all.size()>0)
            realDeviceData=all.get(0);
        return realDeviceData;
    }
    
     public List<RealDeviceInfoBean> findAllByOrgCode(String orgCode) throws Exception {
        List<RealDeviceInfoBean> all = new ArrayList<RealDeviceInfoBean>();
        PreparedStatement pstmt = null;

        String sql = "SELECT * from realdevicedata WHERE orgcode like '" + orgCode + "%' ";

        try {

            pstmt = this.conn.prepareStatement(sql);            
            ResultSet rs = pstmt.executeQuery(); //
            RealDeviceInfoBean realdeviceInfo = null;
            while (rs.next()) { //
                realdeviceInfo = new RealDeviceInfoBean();
                realdeviceInfo.setId(rs.getLong(1));
                realdeviceInfo.setOrgcode(rs.getString(2));
                realdeviceInfo.setBeltspeed(rs.getDouble(3));
                realdeviceInfo.setFlowspeed(rs.getDouble(4));
                realdeviceInfo.setLasttotal(rs.getDouble(5));
                realdeviceInfo.setCurrenttotal(rs.getDouble(6));
                realdeviceInfo.setInitialvalueofhour(rs.getDouble(7));
                realdeviceInfo.setCurrenthourout(rs.getDouble(8));
                Date time = rs.getTimestamp(9);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                realdeviceInfo.setTime(sdf.format(time));
                all.add(realdeviceInfo); //
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return all;
    }
    
    public List<RealDeviceInfoBean> findAll() throws Exception {
        List<RealDeviceInfoBean> all = new ArrayList<RealDeviceInfoBean>();
        PreparedStatement pstmt = null;

        String sql = "SELECT * from realdevicedata";

        try {

            pstmt = this.conn.prepareStatement(sql);            
            ResultSet rs = pstmt.executeQuery(); //
            RealDeviceInfoBean realdeviceInfo = null;
            while (rs.next()) { //
                realdeviceInfo = new RealDeviceInfoBean();
                realdeviceInfo.setId(rs.getLong(1));
                realdeviceInfo.setOrgcode(rs.getString(2));
                realdeviceInfo.setBeltspeed(rs.getDouble(3));
                realdeviceInfo.setFlowspeed(rs.getDouble(4));
                realdeviceInfo.setLasttotal(rs.getDouble(5));
                realdeviceInfo.setCurrenttotal(rs.getDouble(6));
                realdeviceInfo.setInitialvalueofhour(rs.getDouble(7));
                realdeviceInfo.setCurrenthourout(rs.getDouble(8));
                Date time = rs.getTimestamp(9);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                realdeviceInfo.setTime(sdf.format(time));
                all.add(realdeviceInfo); //
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                pstmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return all;
    }

    public int getTotalSize() {
        return size;
    }
}
